import moment from "moment";
import {storage, USER_INFO} from "@/utils/storage";
import router from "@/routers";

export const utils = {
    // 定义时间格式过滤器
    dateFormat(daraStr, pattern) {
        if (!pattern) {
            pattern = 'YYYY-MM-DD HH:mm:ss';
        }
        return moment(daraStr).format(pattern)
    },
}

export const isNeedOpenCard = () => {
    // 根据用户信息去判断跳转
    let userInfo = JSON.parse(storage.get(USER_INFO))
    if(!userInfo){
        // 去登录界面
        router.push('/login')
        return false
    }
    //userCardStatus 0： 未提交开卡信息1:   提交开卡信息未审核完成2: 开卡成功-1: 审核失败-2 ：停用
    if (userInfo.userCardStatus == 1 || userInfo.userCardStatus == 2) {
        return false
    }
    return true
}

export const getTime12HStr = (time) => {
    const timeStr = utils.dateFormat(time, 'HH:mm')
    const h = timeStr.split(":")[0]
    let str = "AM"
    if ((h - 12) > 0) {
        str = "PM"
    }
    return `${utils.dateFormat(time, 'hh:mm')} ${str}`
}

export const getTimeList = () => {
    let time = getStartTimeOfDay()
    let startTime = moment(time)
    let dataList = []
    dataList.push(getTime(startTime))
    for (let i = 1; i < 7; i++) {
        let tempTime =startTime.add(1, 'day')
        dataList.push(getTime(tempTime))
    }
    return dataList
}

function getStartTimeOfDay() {
    const now = new Date()
    const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate())
    return startOfDay.getTime()
}

function getTime(time) {
    let weekStr = getWeekStr(time.weekday())
    let timeStr = time.format("MM月DD日")
    let str = `${timeStr}(${weekStr})`
    return {
        time: time.valueOf(),
        str: str
    }
}

function getWeekStr(num) {
    switch (num) {
        case 0:
            return "日"
        case 1:
            return "一"
        case 2:
            return "二"
        case 3:
            return "三"
        case 4:
            return "四"
        case 5:
            return "五"
        case 6:
            return "六"
        default:
            return ""
    }
}
